//
//  RBShopApplyPageStep3VC.m
//  zhongzhenglu
//
//  Created by mac on 2022/10/24.
//

#import "RBShopApplyPageStep3VC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import "RBThemeConfig.h"
#import <Masonry.h>
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import "UIButton+RBAdd.h"
#import <UIColor+YYAdd.h>
#import "UIViewController+RBAdd.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "RBGradientView.h"
#import "RBGrowablePhotoContainer.h"
#import "RBUploadTool.h"

@interface RBShopApplyPageStep3VC ()

@property(nonatomic, strong) RBNavigationBar *navigationBar;
@property(nonatomic, strong) RBGrowablePhotoContainer *photoContainer;
@property(nonatomic, strong) RBUploadTool *uploadTool;

@end

@implementation RBShopApplyPageStep3VC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self fillData];
}

#pragma mark - Private

- (void)setup
{
    self.fd_prefersNavigationBarHidden = YES;
    RBGradientView *topBgView = [RBGradientView gradientViewWithColors:@[[UIColor colorWithHexString:@"#CADCFF"],[UIColor colorWithHexString:@"#E8F0FF"]] locations:nil vertical:YES];
    [self.view addSubview:topBgView];
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(kRBStatusBarHeight + kRBNavigationBarHeight + Zoom(150));
    }];
    self.navigationBar.hidden = NO;
    
    if(self.response.data.status == 2)
    {
        UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"审核失败,请重新提交" message:self.response.data.fail_reason preferredStyle:UIAlertControllerStyleAlert];
        [vc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:vc animated:YES completion:nil];
    }
}

- (void)fillData
{
    UIView *statusView = ({
        UIView *view = UIView.new;
        NSArray *imgs = @[@"shopApplyPass",@"shopApplyPass",@"shopApplyPass",@"",@""];
        NSArray *names = @[@"注册成功",@"审核通过",@"财务审核",@"保证金审核",@"合同审核"];
        for (NSInteger i = 0; i < imgs.count; i++)
        {
            [view h_view_addView:({
                UIView *view = UIView.new;
                [view view_addView:({
                    UIView *view = UIView.new;
                    view.backgroundColor = [UIColor colorWithHexString:@"#518CFF"];
                    [view rbSetCornerRadius:Zoom(19)];
                    NSString *imgName = imgs[i];
                    if(imgName.length)
                    {
                        UIImageView *imgView = [RanTool imgViewWithMode:1 radius:0 imgName:imgs[i]];
                        [view addSubview:imgView];
                        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
                            make.center.mas_equalTo(0);
                            make.width.mas_equalTo(Zoom(17));
                            make.height.mas_equalTo(Zoom(14));
                        }];
                    }
                    view;
                }) withHeight:Zoom(38) width:Zoom(38) top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:1];
                [view view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:@"#454545" align:1 text:names[i]] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:CGFLOAT_MIN bottom:0 left:0 right:0 centerX:0];
                view;
            }) withHeight:CGFLOAT_MIN width:kRBMainScreenWidth/imgs.count top:0 bottom:0 left:0 right:CGFLOAT_MIN centerY:0];
        }
        view;
    });
    [self.view addSubview:statusView];
    [statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(kRBStatusBarHeight + kRBNavigationBarHeight + Zoom(30));
        make.height.mas_equalTo(Zoom(67));
    }];
    UIScrollView *scrollView = UIScrollView.new;
    [self.view addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(kRBStatusBarHeight + kRBNavigationBarHeight + Zoom(140));
    }];
    [scrollView rbSetContentWidth:kRBMainScreenWidth];
    [scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCorner:UIRectCornerTopLeft | UIRectCornerTopRight radius:Zoom(15)];
        [view view_addView:[RanTool labelWithFontSize:Zoom(18) weight:2 color:@"333333" align:1 text:@"请按以下信息进行转款验证转款成功后提交"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(44) bottom:CGFLOAT_MIN left:Zoom(0) right:Zoom(0) centerX:NO];
        NSArray *titles = @[
            @"保证金为",
            @"账户名称",
            @"开户行",
            @"对公账户",
        ];
                
        NSArray *details = @[
            [NSString stringWithFormat:@"￥%@", self.response.data.baozhengjin],
            self.response.data.company_bank_name,
            self.response.data.company_bank_addr,
            self.response.data.company_bank_code,
        ];
        for(NSInteger i = 0; i < titles.count; i++)
        {
            [view view_addView:({
                UIView *view = UIView.new;
                [view h_view_addView:[RanTool labelWithFontSize:Zoom(16) weight:0 color:@"999999" align:2 text:titles[i]] withHeight:CGFLOAT_MIN width:Zoom(87) top:0 bottom:0 left:0 right:0 centerY:0];
                [view h_view_addView:[RanTool labelWithFontSize:Zoom(16) weight:0 color:@"#333333" align:0 text:details[i]] withHeight:CGFLOAT_MIN width:Zoom(230) top:0 bottom:0 left:Zoom(30) right:0 centerY:0];
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(44) bottom:0 left:0 right:0 centerX:0];
        }
        
        self.photoContainer = [RBGrowablePhotoContainer viewWithMaxNum:1 colNum:2 editable:YES viewWidth:Zoom(230)];
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:[RanTool labelWithFontSize:Zoom(16) weight:0 color:@"999999" align:2 text:@"转款截图"] withHeight:CGFLOAT_MIN width:Zoom(87) top:0 bottom:0 left:0 right:0 centerY:0];
            [view h_view_addView:self.photoContainer withHeight:self.photoContainer.viewHeight width:Zoom(230) top:0 bottom:0 left:Zoom(30) right:0 centerY:0];

            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(44) bottom:0 left:0 right:0 centerX:0];
        
        [view view_addView:({
            UILabel *view = [RanTool labelWithFontSize:Zoom(18) weight:0 color:UIColor.whiteColor align:1 text:@"提交" bgColor:@"#518CFF" radius:Zoom(22)];
            [view rbAddTapGesWithTarget:self action:@selector(submitBtnTapped)];
            view;
        }) withHeight:Zoom(44) width:Zoom(314) top:Zoom(100) bottom:Zoom(40) left:0 right:0 centerX:1];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
}

#pragma mark - Network

#pragma mark - Event

- (void)submitBtnTapped
{
    __weak typeof(self) weak_self = self;
    if(!self.photoContainer.photos.count)
    {
        [self.view showTip:@"请上传转款截图" completion:nil];
        return;
    }
    
    [self.uploadTool uploadData:({
        NSMutableArray *arrayM = NSMutableArray.array;
        NSArray *imgs = self.photoContainer.photos;
        for (UIImage *img in imgs)
        {
            RBUploadModel *model = RBUploadModel.new;
            model.source = img;
            [arrayM addObject:model];
        }
        arrayM;
    }) withCompletion:^(NSArray *data) {
        NSMutableArray *arrayM = NSMutableArray.array;
        for (RBUploadModel *model in data)
        {
            [arrayM addObject:model.res];
        }
        [weak_self commitWithImgs:arrayM];
    }];
}

- (void)commitWithImgs: (NSArray *)imgs
{
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"store_id"] = self.response.data.store_id;
        dicM[@"picurl"] = [imgs componentsJoinedByString:@","];
    } path:@"han_store_step3" post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
        [weak_self.view showTip:@"信息已提交,请等待审核" completion:^{
            [weak_self.navigationController popViewControllerAnimated:YES];
        }];
    } failBlock:^(RBResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Setter

#pragma mark - Getter

- (RBNavigationBar *)navigationBar
{
    if (!_navigationBar)
    {
        self.navigationBar = ({
            RBNavigationBar *bar = RBNavigationBar.new;
            bar.needGradientView = NO;
            [self.view addSubview:bar];
            [bar mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.mas_equalTo(0);
                make.height.mas_equalTo(kRBStatusBarHeight + kRBNavigationBarHeight);
            }];
            [bar.naviBar addBackBtnWithWidth:70 image:[UIImage imageNamed:@"backBlack"]];
            {
                UILabel *view = [RanTool labelWithFontSize:18 weight:0 color:@"222222" align:0 text:@"保证金审核"];
                [bar.naviBar addSubview:view];
                [view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.center.mas_equalTo(0);
                }];
            }
            bar;
        });
    }
    return _navigationBar;
}

- (RBUploadTool *)uploadTool
{
    if(!_uploadTool)
    {
        self.uploadTool = RBUploadTool.new;
    }
    return _uploadTool;
}

@end
